home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_09_04 / 9n04023a < prev    next >
Text File  |  1991-02-04  |  7KB  |  283 lines

  1.  
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <limits.h>
  5.  
  6. #define MAX_NODES 4    /* max number of nodes in array */
  7.  
  8. #define ADD_NODE 'A'
  9. #define CHANGE_NODE 'C'
  10. #define DISPLAY_NODE 'D'
  11. #define EXIT 'E'
  12. #define SEARCH_FNODE 'F'
  13. #define HELP '?'
  14. #define INSERT_NODE 'I'
  15. #define SEARCH_LNODE 'L'
  16. #define COUNT_NODES 'N'
  17. #define REMOVE_NODE 'R'
  18. #define SORT_NODES 'S'
  19. #define DUMP_TABLE 'T'
  20.  
  21. /* ------------------------------------------------------------------- */
  22.  
  23. main()
  24. {
  25.     int ary[MAX_NODES];
  26.  
  27.     int nodes_in_use = 0;
  28.     int index;
  29.     int temp, i, j;
  30.     int inchar = 'x';    /* force initial prompt */
  31.  
  32.     while (1) {
  33.         if (inchar != ' ')
  34.             printf("\nEnter Action Code (%c for help): ", HELP);
  35.         switch(toupper(inchar = getchar())) {
  36.  
  37. /* ------------------------------------------------------------------- */
  38.  
  39.     case EXIT:
  40.         goto end;    /* break won't do so use goto */
  41.  
  42. /* ------------------------------------------------------------------- */
  43.  
  44.     default:
  45.         printf("\n   Invalid command. Please try again\n");
  46.         break;
  47.  
  48. /* ------------------------------------------------------------------- */
  49.  
  50.     case HELP:
  51.         printf("\n   The action codes are:\n");
  52.         printf("\t%c - produces this help message\n", HELP);
  53.         printf("\t%c - Add a new node to the end\n", ADD_NODE);
  54.         printf("\t%c - Change a user-selected node\n", CHANGE_NODE);
  55.         printf("\t%c - Display a user-selected node\n", DISPLAY_NODE);
  56.         printf("\t%c - Exit this program\n", EXIT);
  57.         printf("\t%c - Search for first occurrence\n", SEARCH_FNODE);
  58.         printf("\t%c - Insert a new node\n", INSERT_NODE);
  59.         printf("\t%c - Search for last occurrence\n", SEARCH_LNODE);
  60.         printf("\t%c - Report the number of nodes\n", COUNT_NODES);
  61.         printf("\t%c - Remove a user-selected node\n", REMOVE_NODE);
  62.         printf("\t%c - Sort the nodes in ascending order\n", SORT_NODES);
  63.         printf("\t%c - Show all entries in the table\n", DUMP_TABLE);
  64.         break;
  65.  
  66. /* ------------------------------------------------------------------- */
  67.  
  68.     case '\n':    /* ignore white space on input */
  69.     case ' ' :
  70.     case '\t':
  71.     case '\v':
  72.     case '\f':
  73.         inchar = ' ';    /* indicate white space input */
  74.         break;
  75.  
  76. /* ------------------------------------------------------------------- */
  77.  
  78.     case ADD_NODE:
  79.         if (nodes_in_use == MAX_NODES) {
  80.             printf("\n   Table is full\n");
  81.             break;
  82.         }
  83.  
  84.         printf("\n   Enter new node's value: ");
  85.         scanf("%3d", &ary[nodes_in_use]);
  86.         ++nodes_in_use;
  87.         printf("\n   Node added");
  88.         break;
  89.  
  90. /* ------------------------------------------------------------------- */
  91.  
  92.     case DUMP_TABLE:
  93.         if (nodes_in_use == 0) {
  94.             printf("\n   Table contains no nodes\n");
  95.             break;
  96.         }
  97.  
  98.         printf("\n   Table nodes are as follows:\n");
  99.         for (index = 0; index < nodes_in_use; ++index)
  100.             printf("\tNode %2d => %3d\n", index, ary[index]);
  101.  
  102.         break;
  103.  
  104. /* ------------------------------------------------------------------- */
  105.  
  106.     case COUNT_NODES:
  107.         printf("\tThere are %2d nodes in the table\n", nodes_in_use);
  108.         break;
  109.  
  110. /* ------------------------------------------------------------------- */
  111.  
  112.     case DISPLAY_NODE:
  113.         if (nodes_in_use == 0) {
  114.             printf("\n   Table contains no nodes\n");
  115.             break;
  116.         }
  117.  
  118.         while (1) {
  119.             printf("\n   Enter node number: ");
  120.             scanf("%d", &index);
  121.             if (index >= 0 && index < nodes_in_use)
  122.                 break;
  123.  
  124.             printf("\n   No such node (%d) in table\n", index);
  125.         }
  126.  
  127.         printf("\tNode %2d => %3d\n", index, ary[index]);
  128.         break;
  129.  
  130. /* ------------------------------------------------------------------- */
  131.  
  132.     case CHANGE_NODE:
  133.         if (nodes_in_use == 0) {
  134.             printf("\n   Table contains no nodes\n");
  135.             break;
  136.         }
  137.  
  138.         while (1) {
  139.             printf("\n   Enter node number: ");
  140.             scanf("%d", &index);
  141.             if (index < 0 || index >= nodes_in_use)
  142.                 printf("\n   No such node (%d) in table\n", index);
  143.                 break;
  144.         }
  145.  
  146.         printf("\tNode %2d => %3d\n", index, ary[index]);
  147.         printf("\n   Enter new value: ");
  148.         scanf("%3d", &ary[index]);
  149.         printf("\n   Node changed");
  150.         break;
  151.  
  152. /* ------------------------------------------------------------------- */
  153.  
  154.     case SEARCH_FNODE:
  155.         if (nodes_in_use == 0) {
  156.             printf("\n   Table contains no nodes\n");
  157.             break;
  158.         }
  159.  
  160.         printf("\n   Enter search value: ");
  161.         scanf("%d", &temp);
  162.  
  163.         for (index = 0; index < nodes_in_use; ++index) {
  164.             if (ary[index] == temp) {
  165.                 printf("\tValue %3d found in node %3d\n", 
  166.                     temp, index);
  167.                 goto found1;
  168.             }
  169.         }
  170.  
  171.         printf("\n   No such value (%d) in table\n", temp);
  172. found1:
  173.         break;
  174.  
  175. /* ------------------------------------------------------------------- */
  176.  
  177.     case SEARCH_LNODE:
  178.         if (nodes_in_use == 0) {
  179.             printf("\n   Table contains no nodes\n");
  180.             break;
  181.         }
  182.  
  183.         printf("\n   Enter search value: ");
  184.         scanf("%d", &temp);
  185.  
  186.         for (index = nodes_in_use - 1; index >= 0; --index) {
  187.             if (ary[index] == temp) {
  188.                 printf("\tValue %3d found in node %3d\n", 
  189.                     temp, index);
  190.                 goto found2;
  191.             }
  192.         }
  193.  
  194.         printf("\n   No such value (%d) in table\n", temp);
  195. found2:
  196.         break;
  197.  
  198. /* ------------------------------------------------------------------- */
  199.  
  200.     case SORT_NODES:    /* simple bubble sort */
  201.         if (nodes_in_use == 0) {
  202.             printf("\n   Table contains no nodes\n");
  203.             break;
  204.         }
  205.  
  206.         for (i = nodes_in_use - 2; i >= 0; --i) {
  207.             for (j = 0; j <= i; ++j) {
  208.                 if (ary[j] > ary[j + 1]) {
  209.                     temp = ary[j];
  210.                     ary[j] = ary[j + 1];
  211.                     ary[j + 1] = temp;
  212.                 }
  213.             }
  214.         }
  215.         printf("\n   Nodes sorted");
  216.         break;
  217.  
  218. /* ------------------------------------------------------------------- */
  219.  
  220.     case INSERT_NODE:
  221.         if (nodes_in_use == 0) {
  222.             printf("\n   Table contains no nodes\n");
  223.             break;
  224.         }
  225.  
  226.         if (nodes_in_use == MAX_NODES) {
  227.             printf("\n   Table is full\n");
  228.             break;
  229.         }
  230.  
  231.         while (1) {
  232.             printf("\n   Enter number of node to insert before: ");
  233.             scanf("%d", &index);
  234.             if (index >= 0 && index < nodes_in_use)
  235.                 break;
  236.  
  237.             printf("\n   No such node (%d) in table\n", index);
  238.         }
  239.  
  240.         ++nodes_in_use;
  241.         for (i = nodes_in_use - 1; i > index; --i) {
  242.             ary[i] = ary[i - 1];
  243.         }
  244.         printf("\n   Enter new node's value: ");
  245.         scanf("%3d", &ary[index]);
  246.         printf("\n   Node inserted");
  247.         break;
  248.  
  249. /* ------------------------------------------------------------------- */
  250.  
  251.     case REMOVE_NODE:
  252.         if (nodes_in_use == 0) {
  253.             printf("\n   Table contains no nodes\n");
  254.             break;
  255.         }
  256.  
  257.         while (1) {
  258.             printf("\n   Enter node number: ");
  259.             scanf("%d", &index);
  260.             if (index >= 0 && index < nodes_in_use)
  261.                 break;
  262.  
  263.             printf("\n   No such node (%d) in table\n", index);
  264.         }
  265.  
  266.         for (i = index; i < nodes_in_use; ++i) {
  267.             ary[i] = ary[i + 1];
  268.         }
  269.         --nodes_in_use;
  270.         printf("\n   Node removed");
  271.         break;
  272.  
  273. /* ------------------------------------------------------------------- */
  274.  
  275.         }
  276.     }
  277.  
  278. end:    return (0);
  279. }
  280.  
  281. /* ------------------------------------------------------------------- */
  282.  
  283.